﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

namespace Manager.Tools.RndcConfgen
{
    /// <summary>
    /// rndc-confgen tool user interface form
    /// </summary>
    public partial class RndcConfgenForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="RndcConfgenForm"/> class.
        /// </summary>
        public RndcConfgenForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Handles the Click event of the generateButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void generateButton_Click(object sender, EventArgs e)
        {
            string path = Path.Combine(Properties.Settings.Default.BindDirectoryPath, @"bin\rndc-confgen.exe");
            string arguments = GetArguments();

            string[] startParams = {path, arguments};

            confGenBackgroundWorker.RunWorkerAsync(startParams);
        }

        /// <summary>
        /// Gets the arguments of the tool.
        /// </summary>
        /// <returns></returns>
        private string GetArguments()
        {
            StringBuilder arguments = new StringBuilder();

            if(autoConfCheckBox.Checked)
            {
                arguments.Append("-a ");
            }
            if(keySizeNumericUpDown.Value != 128)
            {
                arguments.AppendFormat("-b {0} ", keySizeNumericUpDown.Text);
            }
            if(keyFileTextBox.Enabled)
            {
                arguments.AppendFormat("-c {0} ", keyFileTextBox.Text);
            }
            if(!string.IsNullOrEmpty(keyNameTextBox.Text))
            {
                arguments.AppendFormat("-k {0} ", keyNameTextBox.Text);
            }
            if(portNumericUpDown.Value != 953)
            {
                arguments.AppendFormat("-p {0} ", portNumericUpDown.Text);
            }
            if(!string.IsNullOrEmpty(addressMaskedTextBox.Text) &&
                !addressMaskedTextBox.Text.StartsWith(" "))
            {
                arguments.AppendFormat("-s {0} ", addressMaskedTextBox.Text);
            }
            if(!string.IsNullOrEmpty(directoryTextBox.Text))
            {
                arguments.AppendFormat("-t {0} ", directoryTextBox.Text);
            }
            if(!string.IsNullOrEmpty(userTextBox.Text))
            {
                arguments.AppendFormat("-u {0} ", userTextBox.Text);
            }

            return arguments.ToString();
        }

        /// <summary>
        /// Handles the Click event of the saveRndcconfToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void saveRndcconfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not yet implemented.");
        }

        /// <summary>
        /// Handles the Click event of the saveToNamedconfToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void saveToNamedconfToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Not yet implemented.");
        }

        /// <summary>
        /// Handles the Click event of the copyButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void copyButton_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(resultTextBox.Text);
        }

        /// <summary>
        /// Handles the CheckedChanged event of the autoConfCheckBox control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void autoConfCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            if(autoConfCheckBox.Checked)
            {
                keyFileTextBox.Enabled = true;
                keyFileChooserButton.Enabled = true;
                directoryTextBox.Enabled = true;
                directoryChooserButton.Enabled = true;
                userTextBox.Enabled = true;
            }
            else
            {
                keyFileTextBox.Enabled = false;
                keyFileChooserButton.Enabled = false;
                directoryTextBox.Enabled = false;
                directoryChooserButton.Enabled = false;
                userTextBox.Enabled = false;
            }
        }

        /// <summary>
        /// Handles the DoWork event of the confGenBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void confGenBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] arguments = (string[])e.Argument;

            ProcessStartInfo startInfo = new ProcessStartInfo(arguments[0], arguments[1]);
            startInfo.CreateNoWindow = true;
            startInfo.ErrorDialog = false;
            startInfo.RedirectStandardOutput = true;
            startInfo.UseShellExecute = false;

            Process process = Process.Start(startInfo);
            using(StreamReader reader = process.StandardOutput)
            {
                e.Result = reader.ReadToEnd();
            }
        }

        /// <summary>
        /// Handles the RunWorkerCompleted event of the confGenBackgroundWorker control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.RunWorkerCompletedEventArgs"/> instance containing the event data.</param>
        private void confGenBackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            resultTextBox.Text = e.Result.ToString();
        }

        /// <summary>
        /// Handles the Click event of the closeToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void closeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
